﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebHoiChoMuaBan.Controllers.Helpers;
using WebHoiChoMuaBan.Controllers.Global;
using WebHoiChoMuaBan.Models;

namespace WebHoiChoMuaBan.Controllers
{
    public abstract class PublicController : Controller
    {

        public bool IsLogin
        {
            get
            {
                if (Session["IsLogin"] == null)
                    return false;
                else return (bool)Session["IsLogin"];
            }
        }

        protected ActionResult HomePage()
        {
            return RedirectToRoute("Default", new { controller = "Home", action = "Index" });
        }

        protected void loadCommon()
        {
            LoadMainMenu();

            if (Session != null && Session["MemId"] != null)
            {
                int? uid = (int)Session["MemId"];
                if (uid != null)
                    LoadMember(uid.Value);
            }

            AdvertHelper hAd = new AdvertHelper();
            ViewData["AdRight"] = hAd.getHotAd(1, 6);
            int pid, cid;

            if (Request.QueryString.Get("pid") == null && Request.QueryString.Get("cid") == null && Request.QueryString.Get("prid") == null)
            {
                //home page

                ViewData["BannerAd"] = hAd.getAdBanners(null, null, Config.BANNER_AD_PER_POS);
            }
            else
            {
                if (Request.QueryString.Get("pid") != null)
                {

                    pid = int.Parse(Request.QueryString.Get("pid") + "");
                    cid = int.Parse(Request.QueryString.Get("cid") + "");
                    if (cid == 0)
                    {
                        //page home
                        ViewData["BannerAd"] = hAd.getAdBanners(pid, 0, Config.BANNER_AD_PER_POS);
                    }
                    else
                    {
                        //page cate
                        ViewData["BannerAd"] = hAd.getAdBanners(pid, cid, Config.BANNER_AD_PER_POS);
                    }
                }
                else
                {
                    //pro
                    if (Request.QueryString.Get("pid") == null && Request.QueryString.Get("cid") == null && Request.QueryString.Get("prid") != null)
                    {
                        NewsProduct pro = new NewsProductHelper().getNewsProductbyID(Request.QueryString.Get("prid"));
                        ViewData["BannerAd"] = hAd.getAdBanners(null, pro.CateId, Config.BANNER_AD_PER_POS);
                    }
                }
            }

            //pid = int.Parse(Request.QueryString.Get("pid") + "");
            //cid = int.Parse(Request.QueryString.Get("cid") + "");

            NewsProductHelper hPro = new NewsProductHelper();
            ViewData["Provinces"] = hPro.LoadAllProvince();

            //Kích hoạt session cho visitor này
            if (Session != null && Session["ViewedNews"] == null)
            {
                HashSet<int> viewedNews = new HashSet<int>();
                Session["ViewedNews"] = viewedNews; //Lưu lại những tin tức mà visitor này đã xem
            }

            if (Session != null && Session["ViewedNewsProduct"] == null)
            {
                HashSet<int> viewedNewsProduct = new HashSet<int>();
                Session["ViewedNewsProduct"] = viewedNewsProduct; //Lưu lại những sản phẩm mà visitor này đã xem
            }
        }

        protected void LoadTimeAndCurrency()
        {
            UtilityModel mTime = new UtilityModel();
            string currency = mTime.time + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tỷ giá: " + mTime.exchange_rate;
            ViewData["Currency"] = currency;
        }

        protected void LoadMainMenu()
        {
            PageTypeHelper hPage = new PageTypeHelper();
            ViewData["MainMenu"] = hPage.getAllPageTypes();
        }
        protected void LoadMainSearch()
        {
            List<PageType> hPage = new PageTypeHelper().getAllPageTypes();
            List<PageType> result = new List<PageType>();
            foreach (PageType p in hPage)
            {
                if (p.PageSearch == 1)
                {
                    result.Add(p);
                }
            }
            PageType pi = new PageType
            {
                PageId = -1,
                PageName = "Bạn bè"
            };
            result.Add(pi);
            base.ViewData["MainSearch"] = result;
        }

        protected ActionResult LoadMember(int id)
        {
            //Load Member có uid này
            MemberHelper hMem = new MemberHelper(id);
            if (hMem.Member == null)
                return HomePage();

            if (Session["MemId"] != null && hMem.Member.MemId == (int)Session["MemId"])
                ViewData["IsOwner"] = true;
            else
                ViewData["IsOwner"] = false;
            ViewData["hMem"] = hMem;

            return null;
        }
    }
}
